package br.com.prime.model.bussines;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import br.com.prime.model.ConnectionFactory;
import br.com.prime.model.pojo.Aluno;
import br.com.prime.model.pojo.Endereco;

public class EnderecoDAO {

	private Connection connection;

	public EnderecoDAO() {
		try {
			this.connection = new ConnectionFactory().getConnection();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public List<Endereco> getAll() {
		List<Endereco> enderecos = new ArrayList<Endereco>();
		String sql = "SELECT * FROM ENDERECO";
		try {
			PreparedStatement stmt = this.connection.prepareStatement(sql);
			ResultSet rs = stmt.executeQuery();
			enderecos = montarObjeto(rs);
			rs.close();
			stmt.close();
			return enderecos;
		} catch (SQLException e) {
			e.printStackTrace();
			return enderecos;
		}
	}

	public List<Endereco> montarObjeto(ResultSet rs) {
		List<Endereco> enderecos = new ArrayList<Endereco>();
		try {
			while (rs.next()) {
				Endereco endereco = new Endereco();
				endereco.setID_ENDERECO(rs.getInt("iD_ENDERECO"));
				endereco.setRUA(rs.getString("RUA"));
				endereco.setNUMERO(rs.getInt("NUMERO"));
				endereco.setBAIRRO(rs.getString("BAIRRO"));
				endereco.setCIDADE(rs.getString("CIDADE"));
				endereco.setESTADO(rs.getString("ESTADO"));

				enderecos.add(endereco);
			}
			return enderecos;
		} catch (Exception e) {
			e.printStackTrace();
			return enderecos;
		}
	}
}